package com.xigua.xcar.phoneindex;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.xigua.xcar.R;
import com.xigua.xcar.base.BaseActivity;
import com.xigua.xcar.constant.BrandConstants;
import com.xigua.xcar.constant.Constants;
import com.xigua.xcar.data.PhoneIndexListData;
import com.xigua.xcar.phonenumlist.PhoneNumListActivity;
import com.xigua.xcar.widgets.phoneIndexList.PhoneIndexListAdapter;

public class PhoneIndexActivity extends BaseActivity {
	private static final String tag = "PhoneIndexActivity";
	private ListView mList = null;
	private PhoneIndexListAdapter mAdapter = null;
	private ArrayList<PhoneIndexListData> mArray = null;

	@Override
	protected void finalize() throws Throwable {
		// 注销的注意事项
		super.finalize();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i(tag, "onCreate");
		super.onCreate(savedInstanceState);
		setView(R.layout.phoneindex);
		setTopBarTitle("常用");
		setTitleBar(R.drawable.title_button, "返回", 0, "");
		mList = (ListView) findViewById(R.id.listview_phoneindex_main);
		mArray = new ArrayList<PhoneIndexListData>();
		mAdapter = new PhoneIndexListAdapter(this);
		initData();
		mAdapter.setData(mArray);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view, int arg2,
					long arg3) {
				PhoneIndexListData data = (PhoneIndexListData) view.getTag();
				if(1 == data.getType()){
					return ;
				}
				Intent intent = new Intent(PhoneIndexActivity.this,
						PhoneNumListActivity.class);
				intent.putExtra(Constants.COMMON_FROMWHERE, data.getIndex() + 2);
				PhoneIndexActivity.this.startActivity(intent);
			}
		});
	}

	private void initData() {
		int index = -1;
		for (int i = 0; i < BrandConstants.BRANDS.length; i++) {
			for (int j = 0; j < BrandConstants.BRANDS[i].length; j++) {
				PhoneIndexListData data = new PhoneIndexListData();
				if (0 == j) {
					data.setType(1);
				} else {
					index++;
				}
				data.setContent(BrandConstants.BRANDS[i][j]);
				data.setIndex(index);
				mArray.add(data);
			}
		}
	}

	@Override
	protected void handleTitleBarEvent(int buttonId) {
		switch (buttonId) {
		case Constants.LEFTBUTTONID: {
			this.finish();
			break;
		}
		case Constants.RIGHTBUTTONID: {
			break;
		}
		}
	}
}
